﻿namespace RainbowNavBar.View
{
    /// <summary>
    /// 消息窗口主体
    /// </summary>
    public partial class EPopupBox : Window
    {
        /// <summary>
        /// 消息数据
        /// </summary>
        public class MessageData
        {
            /// <summary>
            /// 消息图标
            /// </summary>
            public string MessageIcon { get; set; }

            /// <summary>
            /// 消息文本
            /// </summary>
            public string MessageText { get; set; }

            /// <summary>
            /// 消息背景
            /// </summary>
            public Brush MessageBackground { get; set; }
        }

        /// <summary>
        /// 关闭窗口定时器
        /// </summary>
        private readonly DispatcherTimer _timer;

        /// <summary>
        /// 记录窗口坐标位置
        /// </summary>
        private readonly Window MBox = new();

        /// <summary>
        /// 颜色值结构体对象
        /// </summary>
        private readonly EMColorData _EMCD = new();

        /// <summary>
        /// 消息类型
        /// </summary>
        public enum EMColor
        {
            Success,
            Error,
            Warning,
            Info,
            Default,
        }

        /// <summary>
        /// 结构体存储对应的背景颜色值
        /// </summary>
        private readonly struct EMColorData
        {
            public readonly Brush Success;
            public readonly Brush Error;
            public readonly Brush Warning;
            public readonly Brush Info;
            public readonly Brush Default;

            public EMColorData()
            {
                this.Success = new SolidColorBrush(Color.FromRgb(76, 175, 80));
                this.Error = new SolidColorBrush(Color.FromRgb(244, 67, 54));
                this.Warning = new SolidColorBrush(Color.FromRgb(255,140,0));
                this.Info = new SolidColorBrush(Color.FromRgb(25, 118, 210));
                this.Default = new SolidColorBrush(Color.FromRgb(169, 169, 169));
            }
        }

        /// <summary>
        /// 关闭弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseMessage(object sender, RoutedEventArgs e)
        {
            // 将数据置为Null
            SystemGlobalStorage.ApplicationTip = null;
            // 启动动画
            Storyboard slideInStoryboard = (Storyboard)this.Resources["SlideInUpAnimationUp"];
            slideInStoryboard.Completed += delegate
            {
                _timer.Stop();
                // 关闭提示框
                //this.Close();
            };
            slideInStoryboard.Begin();
        }

        /// <summary>
        /// 无参构造
        /// </summary>
        public EPopupBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 传入父窗口
        /// </summary>
        /// <param name="owner"></param>
        public EPopupBox(Window owner, EMColor messageType, string message = "")
        {
            InitializeComponent();

            this.Owner = (MBox = owner);
            this.Left = MBox.Left + ((MBox.Width / 2) - (this.Width / 2));
            this.Top = MBox.Top + 40;
            // 启动动画
            Storyboard slideInStoryboard = (Storyboard)this.Resources["SlideInUpAnimationDown"];
            slideInStoryboard.Begin();
            // MessageText.Text = message;

            // 设置数据源
            this.DataContext = new MessageData()
            {
                MessageText = message,
                MessageBackground = messageType switch
                {
                    EMColor.Success => _EMCD.Success,
                    EMColor.Error => _EMCD.Error,
                    EMColor.Warning => _EMCD.Warning,
                    EMColor.Info => _EMCD.Info,
                    EMColor.Default => _EMCD.Default,
                    _ => _EMCD.Default
                },

                MessageIcon = messageType switch
                {
                    EMColor.Success => "check",
                    EMColor.Error => "close",
                    EMColor.Warning => "warning",
                    EMColor.Info =>"send",
                    EMColor.Default => "chat",
                    _ => "chat"
                },
            };

            // 创建定时器
            _timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(2)  // 2 秒后触发
            };

            _timer.Tick += (sender, args) =>
            {
                // 将数据置为Null
                SystemGlobalStorage.ApplicationTip = null;
                // 启动动画
                Storyboard slideInStoryboard = (Storyboard)this.Resources["SlideInUpAnimationUp"];
                slideInStoryboard.Completed += delegate
                {
                    // 停止定时器
                    _timer.Stop();

                    // 关闭提示框
                    //this.Close();
                };
                slideInStoryboard.Begin();
            };

            _timer.Start();  // 启动定时器
        }

        /// <summary>
        /// 鼠标点击关闭弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            CloseMessage(null, null);
        }

    }
}
